﻿using CRMG.Container.Source.BO;
using CRMG.Container.Source.Helper;
using CRMG.Container.Source.OpManager;
using CRMG.Container.Source.Variables;
using CRMG.ResourceManagement.Resources;
using Guru.ErrorHandling;
using Guru.Framework.Util;
using Guru.TransactionLogger;
using Guru.TransactionLogger.Source.BO;
using System;
using System.Data;
using System.Globalization;
using System.Resources;
using System.Threading;
using Telerik.WinControls.UI;

namespace CRMG.Container.Views.Training
{
    public partial class FrmTrainClass : RadForm
    {
        ResourceManager formRes;
        ResourceManager messageRes;
        TrainManager trainMan;
        DataTable dtTrainClass;
        int pClassId = -1;
        bool isUpdated = false;
        bool isTransactionNotCompleted = true;

        public FrmTrainClass() : this(-1) { }

        public FrmTrainClass(int TrainClassId)
        {
            try
            {
                Thread.CurrentThread.CurrentUICulture = new CultureInfo(AppVariables.ApplicationCulture);
                InitializeComponent();
                formRes = new ResourceManager("CRMG.Container.Resources.ControlResources.WinFormStrings", typeof(FrmTrainClass).Assembly);
                messageRes = new ResourceManager("CRMG.Container.Resources.MessageResources.AppMessages", typeof(FrmTrainClass).Assembly);
                trainMan = new TrainManager();
                pClassId = TrainClassId;
                dtTrainClass = trainMan.GetTrainClasses();
                this.Text = formRes.GetString("FrmTrainClass.Caption");
                lblClassName.Text = formRes.GetString("FrmTrainClass.lblClassName");
                lblClassCode.Text = formRes.GetString("FrmTrainClass.lblClassCode");
                btnSave.Text = formRes.GetString("Add");
                btnCancel.Text = formRes.GetString("Cancel");
                if (pClassId != -1)
                {
                    btnSave.Text = formRes.GetString("Update");
                    txtClassName.Text = DataUtil.GetObjectWithSelectedColumn(dtTrainClass, "TrainClassName", pClassId, "OBJID").ToStr();
                    txtClassCode.Text = DataUtil.GetObjectWithSelectedColumn(dtTrainClass, "TrainClassCode", pClassId, "OBJID").ToStr();
                }
            }
            catch (Exception exc)
            {
                LogEntry logger = new LogEntry(AppVariables.CurrentUser.OBJID)
                {
                    ErrorCode = 2029,
                    Message = exc.Message,
                    StackTrace = exc.StackTrace,
                    Title = Rc.GetString(2029)
                };
                logger.Write();
                ControlUtil.ShowErrorMessage(
                      messageRes.GetString("FrmTrainClass.Ctor.Error"),
                      messageRes.GetString("Error"));
            }
            finally
            {
                btnSave.Enabled = false;
                isUpdated = false;
            }
        }

        private void ClassNameChange(object sender, EventArgs e)
        {
            isUpdated = true;
            btnSave.Enabled = (txtClassName.Text.Length * txtClassCode.Text.Length) > 0 && isTransactionNotCompleted;
        }

        private void Save(object sender, EventArgs e)
        {
            try
            {
                object obj = DataUtil.GetObjectWithSelectedColumn(dtTrainClass, "TrainClassName", txtClassName.Text, "OBJID");
                int trnId = ConverterUtil.Convert2Int(obj);
                if (trnId > 0 && trnId != pClassId)
                {

                    ControlUtil.ShowWarningMessage(
             messageRes.GetString("FrmTrainClass.Save.TrainClassAlreadyExist"),
             messageRes.GetString("Warning"));
                    return;
                }
                obj = DataUtil.GetObjectWithSelectedColumn(dtTrainClass, "TrainClassCode", txtClassCode.Text, "OBJID");
                trnId = ConverterUtil.Convert2Int(obj);
                if (trnId > 0 && trnId != pClassId)
                {

                    ControlUtil.ShowWarningMessage(
             messageRes.GetString("FrmTrainClass.Save.TrainCodeAlreadyExist"),
             messageRes.GetString("Warning"));
                    return;
                }
                else
                {
                    TrainClass trainclass = new TrainClass()
                    {
                        TrainClassCode = txtClassCode.Text,
                        TrainClassName = txtClassName.Text
                    };
                    int Id = 0;
                    if (pClassId == -1)
                    {
                        Id = trainMan.AddTrainClass(trainclass);
                    }
                    else
                    {
                        if (isUpdated == true)
                        {
                            trainclass.OBJID = pClassId;
                            trainMan.UpdateTrainClass(trainclass);
                        }
                    }
                    isTransactionNotCompleted = false;
                    btnSave.Enabled = false;
                    btnCancel.Text = formRes.GetString("Close");
                    TransactionLogEntry transLogger = new TransactionLogEntry(AppVariables.CurrentUser.OBJID)
                    {
                        FormName = this.Name,
                        TransactionType = pClassId == -1 ? TransactionTypes.Insert : TransactionTypes.Update,
                        ControlName = btnSave.Text,
                        ControlTag = btnSave.Tag != null ? btnSave.Tag.ToString() : "",
                        LogObject = pClassId == -1 ? Id : pClassId
                    };
                    transLogger.Insert();
                    ControlUtil.ShowInfoMessage(
                     messageRes.GetString("FrmTrainClass.Save.Success"),
                     messageRes.GetString("Info"));
                }
            }
            catch (Exception exc)
            {
                LogEntry logger = new LogEntry(AppVariables.CurrentUser.OBJID)
                {
                    ErrorCode = 2030,
                    Message = exc.Message,
                    StackTrace = exc.StackTrace,
                    Title = Rc.GetString(2030)
                };
                logger.Write();
                ControlUtil.ShowErrorMessage(
                      messageRes.GetString("FrmTrainClass.Save.Error"),
                      messageRes.GetString("Error"));
            }
        }


        private void Cancel(object sender, EventArgs e)
        {
            Close();
        }

    }
}
